<template>
  <div class="messageList">
    <div class="messageList-head">
      <div class="search-box">
        <!-- <div class="select-box">
          全部<span class="font_family icon-gengduo"></span>
        </div> -->
        <div class="search-input">
          <span class="font_family icon-sousuo"></span>
          <input
            type="text"
            class="input"
            @keydown.enter="handleSearch"
            v-model="searchValue"
            placeholder="标题"
          />
          <span
            class="font_family icon-shanchu"
            v-show="isClear"
            @click="searchValue = ''"
          ></span>
        </div>
        <div class="date" :value="date" @click="show = true">
          <img src="@/assets/svg/rili.svg" class="img" />
        </div>
      </div>
      <!-- messageList-tabs -->
      <div class="messageList-tabs">
        <div class="tabs-scroll">
          <div
            class="item"
            v-for="(item, index) in menuList"
            :key="index"
            :class="item.active ? 'active' : ''"
            @click="parentMenu(index)"
          >
            {{ item.msg_group }}
          </div>
        </div>
      </div>
    </div>
    <!-- messageList-content -->
    <div class="messageList-content">
      <div class="menu-list">
        <div
          class="item"
          v-for="(item, index) in childMenu.data"
          :key="index"
          :class="item.selected ? 'active' : ''"
          @click="childTab(index)"
        >
          {{ item.msg_type }}
        </div>
      </div>
      <div class="content-list">
        <div v-if="type == 0" class="list-scroll">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <div
              class="item"
              v-for="(item, index) in massageData"
              :key="index"
              @click="handleDetail(item.id, item.title)"
            >
              <div v-if="item.title != 'IT服务单（开发需求）'" class="tips">
                <p>该流程不支持预览，请前往PC端查看</p>
              </div>
              <div class="item-content">
                <div class="title">{{ item.title }}</div>
                <div class="time">{{ item.date }}</div>
              </div>
              <div class="item-desc">
                <div class="desc">
                  {{ item.subject }}
                </div>
                <div class="name">{{ item.username }}</div>
              </div>
              <div class="current">
                <p>当前节点：{{ item.node_name }}</p>
              </div>
            </div>
          </van-list>
          <!-- <div
            class="item"
            v-for="(item, index) in massageData"
            :key="index"
            @click="handleDetail(item.id)"
          >
            <div class="item-content">
              <div class="title">{{ item.title }}</div>
              <div class="desc">
                {{ item.subject }}
              </div>
            </div>
            <div class="item-desc">
              <div class="name">{{ item.username }}</div>
              <div class="time">{{ item.date }}</div>
            </div>
            
          </div> -->
        </div>
        <div v-if="type != 0" class="list-scroll">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <div
              class="item"
              v-for="(item, index) in massageData"
              :key="index"
              @click="handleDetail(item.id, item.title)"
            >
              <div class="item-content">
                <div class="title">{{ item.title }}</div>
                <div class="time">{{ item.send_date }}</div>
              </div>
              <div class="item-desc">
                <div class="desc">
                  {{ item.msg_content }}
                </div>
                <div class="name">{{ item.send_name }}</div>
              </div>
              <span v-if="item.is_read == '未读'"></span>
            </div>
          </van-list>
        </div>
      </div>
    </div>
    <van-calendar
      color="#009966"
      :min-date="minDate"
      v-model="show"
      @confirm="onConfirm"
    />
    <goback></goback>
  </div>
</template>
<script>
import { messageList, messageTypes, serviceList } from "@/api/message.js";
import Goback from "@/components/goback.vue";
export default {
  components: {
    Goback,
  },
  data() {
    return {
      show: false,
      searchValue: "",
      date: "",
      menuList: "",
      childMenu: "",
      page: 1,
      pagesize: 10,
      type: 0,
      massageData: [],
      loading: false,
      finished: false,
      childMenuId: "",
      minDate: new Date(1900, 1, 1),
      pathType: 0, //接口传参
      pathIndex: 0, //接口传参
      isClear:false
    };
  },
  watch:{
    searchValue(newVal){
      if(newVal == ''){
        this.isClear = false;
        this.handleSearch();
      }else{
        this.isClear = true
      }
    }
  },
  mounted() {
    this.getTypes();
    if (this.$route.query.type) {
      this.pathType = this.$route.query.type;
    }
    if (this.$route.query.index) {
      this.pathIndex = this.$route.query.index;
    }
  },
  methods: {
    handleDetail(listid, title) {
      console.log(title);

      if (this.type == 0) {
        if (title == "IT服务单（开发需求）") {
          this.$router.push({
            path: "/service/detail",
            query: {
              id: listid,
            },
          });
        }
      } else {
        this.$router.push({
          path: "/message/detail",
          query: {
            id: listid,
          },
        });
      }
    },
    onConfirm(date) {
      this.show = false;
      this.date = this.formatDate(date);
      // 切换类型设置可以加载更多
      this.loading = true;
      this.finished = false;
      this.page = 1;
      this.massageData = [];
      if (this.type == 0) {
        this.getlcData(this.childMenuId);
      } else {
        this.getlbData(this.childMenuId);
      }
    },
    formatDate(date) {
      let month =
        date.getMonth() + 1 > 9
          ? date.getMonth() + 1
          : "0" + (date.getMonth() + 1);
      let day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
      return `${date.getFullYear()}-${month}-${day}`;
    },
    
    getTypes() {
      this.massageData = [];
      messageTypes().then((res) => {
        console.log(res);
        if (res.status == "success") {
          for (var i = 0; i < res.data.length; i++) {
            res.data[i].active = i === 0;
            for (var j = 0; j < res.data[i].data.length; j++) {
              res.data[i].data[j].selected = j === 0;
            }
          }
          this.menuList = res.data;
          this.childMenu = res.data[0];
          this.childMenuId = this.childMenu.data[0].id;
          // this.getlcData(this.childMenu.data[0].id);
          if (this.pathType != 0) {
            this.parentMenu(this.pathIndex);
          } else {
            if (this.pathIndex) {
              this.childTab(this.pathIndex);
            }
          }
        }
      });
    },
    // 点击父tab
    parentMenu(index) {
      // 切换类型设置可以加载更多
      this.loading = true;
      this.finished = false;
      this.page = 1;
      this.date = "";
      this.massageData = [];
      for (var i = 0; i < this.menuList.length; i++) {
        this.menuList[i].active = index == i;
      }
      this.childMenu = this.menuList[index];
      for (var j = 0; j < this.childMenu.data.length; j++) {
        this.childMenu.data[j].selected = j === 0;
      }

      this.childMenuId = this.childMenu.data[0].id;
      if (index == 0) {
        this.type = 0;
        this.getlcData(this.childMenu.data[0].id);
      } else {
        this.type = 1;
        this.getlbData(this.childMenu.data[0].id);
      }
    },
    //点击子tab
    childTab(index) {
      // 切换类型设置可以加载更多
      this.loading = true;
      this.finished = false;
      this.page = 1;
      this.date = "";
      this.massageData = [];
      for (var i = 0; i < this.childMenu.data.length; i++) {
        this.childMenu.data[i].selected = index == i;
      }
      this.childMenuId = this.childMenu.data[index].id;
      if (this.type == 0) {
        this.getlcData(this.childMenu.data[index].id);
      } else {
        this.getlbData(this.childMenu.data[index].id);
      }
    },
    // 搜索框
    handleSearch(){
      this.loading = true;
      this.finished = false;
      this.page = 1;
      this.massageData = [];
      if (this.type == 0) {
        this.getlcData(this.childMenuId);
      } else {
        this.getlbData(this.childMenuId);
      }
    },
    onLoad() {
      let that = this;
      that.page = that.page + 1;
      if (that.type == 1) {
        that.getlbData(that.childMenuId);
      }
    },
    getlbData(id) {
      messageList({
        title: this.searchValue,
        daytime: this.date,
        page: this.page,
        pagesize: this.pagesize,
        type_id: id,
      }).then((res) => {
        console.log(res.data[0].msg_info);
        if (res.status == "success") {
          if (
            res.data[0].msg_info.length == 0 ||
            res.data[0].msg_info_count < this.pagesize
          ) {
            this.finished = true;
          }
          this.massageData = this.massageData.concat(res.data[0].msg_info);
          this.loading = false;
        }
      });
    },
    getlcData(id) {
      serviceList({
        search: this.searchValue,
        date: this.date,
        page: 1,
        key: id,
      }).then((res) => {
        console.log(res);
        if (res.status == "success") {
          // if(res.data.data.length == 0 || res.data.data.length<this.pagesize){
          //   this.finished = true
          // }
          this.massageData = this.massageData.concat(res.data);
          this.loading = false;
          this.finished = true;
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.messageList {
  background: #f7f9f9;
  padding-top: 102px;
  min-height: calc(100vh - 112px - env(safe-area-inset-bottom));
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
}
// messageList-head
.messageList-head {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: #fff;
  .search-box {
    display: flex;
    align-items: center;
    padding: 15px;
    .select-box {
      width: 70px;
      margin-right: 5px;
      margin-left: 15px;
      height: 36px;
      line-height: 36px;
      font-size: 13px;
      color: #313541;
      span {
        display: inline-block;
        font-weight: normal;
        transform: rotate(90deg);
        margin-left: 7px;
        font-size: 10px;
      }
    }
    .search-input {
      flex-grow: 1;
      position: relative;
      .input {
        width: 100%;
        display: block;
        height: 36px;
        padding: 6px 12px 6px 36px;
        border: 0;
        font-size: 14px;
        box-sizing: border-box;
        background: #f8f8f8;
        border-radius: 7px;
        &:focus {
          outline: none;
        }
      }
      .icon-sousuo {
        position: absolute;
        top: 50%;
        left: 10px;
        margin-top: -7px;
        font-size: 15px;
        color: #8f92a1;
      }
      .icon-shanchu {
        position: absolute;
        top: 7px;
        padding: 10px;
        right: 5px;
        margin-top: -7px;
        font-size: 15px;
        color: #8f92a1;
      }
    }
    .date {
      padding: 7px 15px;
      margin-left: 8px;
      background: #f8f8f8;
      border-radius: 8px;
      img {
        width: 19px;
      }
    }
  }
  // messageList-tabs
  .messageList-tabs {
    width: 100vw;
    overflow-y: scroll;
    .tabs-scroll {
      width: auto;
      white-space: nowrap;
    }
    .item {
      display: inline-block;
      text-align: center;
      line-height: 40px;
      font-size: 13px;
      color: #313541;
      margin: 0 15px;

      &.active {
        font-size: 14px;
        font-weight: bold;
        position: relative;
        color: #009966;
        &:after {
          position: absolute;
          content: "";
          left: 50%;
          bottom: 0;
          width: 20px;
          height: 1px;
          margin-left: -10px;
          background: #009966;
        }
      }
    }
  }
}
// messageList-content
.messageList-content {
  position: fixed;
  top: 107px;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  .menu-list {
    width: 65px;
    background: #fff;
    .item {
      height: 40px;
      line-height: 40px;
      color: #313541;
      font-size: 12px;
      text-align: center;
      &.active {
        background: transparent;
        color: #009966;
        background: #f7f9f9;
        font-weight: bold;
      }
      &.prev-active {
        position: relative;
        &:before {
          position: absolute;
          content: "";
          right: 0;
          bottom: 0;
          width: 14px;
          height: 14px;
          background: #f7f9f9;
        }
        &:after {
          position: absolute;
          content: "";
          right: 0;
          bottom: 0;
          width: 14px;
          height: 14px;
          border-radius: 0 0 8px 0;
          background: #fff;
        }
      }
      &.next-active {
        position: relative;
        &:before {
          position: absolute;
          content: "";
          right: 0;
          top: 0;
          width: 14px;
          height: 14px;
          background: #f7f9f9;
        }
        &:after {
          position: absolute;
          content: "";
          right: 0;
          top: 0;
          width: 14px;
          height: 14px;
          border-radius: 0 8px 0 0;
          background: #fff;
        }
      }
    }
  }
  .content-list {
    flex: 1;
    padding: 8px;
    overflow-x: auto;
    .list-scroll {
      height: auto;
    }
    .item {
      background: #fff;
      border-radius: 8px;
      margin-bottom: 8px;
      position: relative;
      .tips {
        margin: 0 15px;
        border-bottom: 1px solid #f2f2f2;
        p {
          margin: 0;
          color: #fa4e3e;
          font-size: 12px;
          line-height: 36px;
        }
      }
      span {
        position: absolute;
        top: 20px;
        right: 20px;
        display: inline-block;
        width: 7px;
        height: 7px;
        background: #fa4e3e;
        border-radius: 50%;
      }
    }
    .item-content {
      // height: 47px;
      padding: 15px;
      padding-bottom: 0;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      .title {
        height: 22px;
        font-size: 16px;
        font-weight: bold;
        line-height: 22px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .time {
        min-width: 70px;
        text-align: right;
        font-size: 12px;
        color: #939dad;
      }
    }
    .item-desc {
      display: flex;
      line-height: 30px;
      padding: 0 15px 10px;
      justify-content: space-between;
      align-items: center;
      .desc {
        font-size: 13px;
        color: #313541;
        height: 17px;
        line-height: 17px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .name {
        min-width: 60px;
        text-align: right;
        font-size: 13px;
        color: #313541;
      }
    }
    .current {
      margin: 0 15px;
      border-top: 1px solid #f2f2f2;
      p {
        color: #313541;
        font-size: 13px;
        padding: 7px 0;
        margin: 0;
      }
    }
  }
}
</style>